package com.gem.eolane.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gem.eolane.entity.News;
import com.gem.eolane.mapper.NewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: cz
 * @Date: 2019/4/10 9:21
 * @Description: 新闻服务层
 */
@Service
public class NewsService extends ServiceImpl<NewsMapper, News> {
   //当前页(初始页)
    public static Integer CURRENT_PAGE = 1;
    //每页的记录数
    public static Integer PAGE_SIZE = 4;

    //注入
    @Autowired
    private NewsMapper newsMapper;

    //分页查询
    public IPage<News> findPageList(News news,Integer page, Integer size) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (page != null) {
            CURRENT_PAGE = page;
        }
        if (size != null) {
            PAGE_SIZE = size;
        }
        if (news!= null && news.getTitle()!= null && news.getTitle() != "") {
            queryWrapper.like("title",news.getTitle());
        }
        IPage<News> pageNews = newsMapper.selectPage(
               new Page<>(CURRENT_PAGE,PAGE_SIZE),
               queryWrapper);
        return pageNews;
    }

    //预览时保存新闻
    public void saveNews(News news) {
        news.setDelIdent(2);
        if (news.getId() != null) {
            newsMapper.updateNewsBysubld(news);
        } else {
            newsMapper.insert(news);
        }
    }


    //查找最新新添加的那条新闻
    public News preShowNews(){
        return newsMapper.findLasterNews();
    }

    //返回上一级
    public void backUp(Integer preId) {
        newsMapper.backUp(preId);
    }

    //返回上一级时回显数据
    public News findNewBysubld(Integer subld) {
        return newsMapper.findNewBysubld(subld);
    }


    //确认提交将标识符改为显示的0
    public void confirmSubmit(int preId){
        newsMapper.confirmSubmit(preId);
    }

    //删除新闻
    public void deleteNewsById(Integer id) {
        newsMapper.deleteNewsById(id);
    }

    //查询4条最新记录公司新闻
    public List<News> findCompanyNewsDesc(){
        List<News> list = newsMapper.findCompanyNewsDesc();
        return list;

    }
    //从后查询4条最新行业新闻
    public List<News> findIndustryNewsDesc(){
        List<News> list = newsMapper.findIndustryNewsDesc();
        return list;
    }
    //查询最新的一条公司新闻
    public News findNewCompanyNews(){
        News news = newsMapper.findNewCompanyNews();
        return news;
    }
    //分页查询公司新闻
    public List<News> findPageCompanyNews(int currIndex, int pageSize){

        currIndex=(currIndex-1)*pageSize;
        return  newsMapper.findPageCompanyNews(currIndex,pageSize);
    }
    //分页查询行业新闻
    public List<News> findPageIndustryNews(int currIndex,int pageSize){

        currIndex=(currIndex-1)*pageSize;
        return  newsMapper.findPageIndustryNews(currIndex,pageSize);
    }


    //查询公司新闻总记录数
    public   int getCompanyTotal(){
        return newsMapper.getCompanyTotal();
    }
    //查询行业新闻总记录数
    public   int getIndustryTotal(){
        return newsMapper.getIndustryTotal();
    }

    //根据id查找新闻内容
    public News findNewsContent(int id){
        return  newsMapper.findNewsContent(id);
    }
}
